package scr 
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	import ru.gosoplata.model.TmpModel;
	/**
	 * ...
	 * @author a.orleansky
	 */
	public class ScreenWCDirection extends ScreenBase implements IScreen
	{
		private var left:Loader;
		private var right:Loader;
		private var _showArrorTimer:Timer = new Timer(10000, 1);
		//private var _openTimer:Timer = new Timer(700, 3);
		
		public function ScreenWCDirection() 
		{
			super("wc_direction");
		}
		
		override protected function init(e:Event):void 
		{
			super.init(e);
			
			left = new Loader();
			var url:URLRequest = new URLRequest("img/toilet/arrow_left.png");
			left.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void { } );
			left.load(url);
			left.x = 150;
			left.y = 300;
			
			right = new Loader();
			url = new URLRequest("img/toilet/arrow_right.png");
			right.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void { } );
			right.load(url);
			right.x = left.x;
			right.y = left.y;
			
			addChild(left);
			addChild(right);
			
			loadBanner(TmpModel.IMG_FOLDER + "/blagorim_za_ispolzovanie/images/tualet_reklama_title.png");
			
			_showArrorTimer.addEventListener(TimerEvent.TIMER_COMPLETE, initWithDelay);
			
			//_openTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onOpenFail);
			//_openTimer.addEventListener(TimerEvent.TIMER, onResendOpen);
		}
		/*
		private function onOpenFail(e:TimerEvent):void 
		{
			var doorName:String = (_forOpen==1)?'left':"right";
			
				TmpModel.acceptor.writeLog('WC ERROR: cant open: ' + doorName + ' (' + _forOpen + '). from atempt ' + _openTimer.currentCount + '.');
				
				//var nearDoor:String = (_forOpen == 1)?"right":"left";
				//if (!_chekNearDoor && TmpModel.wcData[nearDoor] == 0) 
				//{
					//TmpModel.acceptor.writeLog('WC ERROR: '+nearDoor+' is free. try it.');
					//_chekNearDoor = true;
					//_forOpen = (_forOpen == 1)?2:1; 
					
					//TmpModel.acceptor.wcOpen(_forOpen, onWCStatus);
				//}else
				//{
					//TmpModel.acceptor.writeLog('WC ERROR: no one door be opened or free.');
					//TODO: ошибка!!!!
					initWithDelay();
				//}
		}
		*/
		private function onResendOpen(e:TimerEvent=null):void 
		{
			TmpModel.acceptor.wcOpen(_forOpen, onWCStatus);
		}
		
		override public function reDraw():void
		{
			//var doornum:Number = TmpModel.wcData.DOORNUM;
			//var sw:Boolean = (doornum == 1);
			//if (TmpModel.readDataFromSO("tst")=="1"){
				//left.visible = sw;
				//right.visible = !sw;
				//TmpModel.acceptor.wcOpen(doornum);
			//}else {
				left.visible = false;
				right.visible = false;
				//TmpModel.acceptor.wcOpen(1, waitanswer);// TODO: получается мы открываем обе двери????
			//}
			//TmpModel.writeDataToSO("false","swwc");
			//mytm.start();
			//mytm.addEventListener(TimerEvent.TIMER_COMPLETE, initWithDelay);
			
			_forOpen = 0;
			//_chekNearDoor = false
			
			TmpModel.acceptor.wcStatus(	function (status:Object):void 
												{
													TmpModel.wcData.parseStatus(status['WcStatus']);
													TmpModel.debugOutput.add(JSON.stringify(status));
													if (TmpModel.wcData.availabel) 
													{
														_forOpen = TmpModel.wcData.getFreeDoor();
														//_openTimer.reset();
														//_openTimer.start();
														onResendOpen();
													}else 
													{
														TmpModel.screenController.showScreenByName("wc_closed");
														
														TmpModel.acceptor.writeLog('WC ERROR: туалет оплачен, но все кабинки заняты!');
													}
												});
		}
		private var _forOpen:uint = 0;
		//private var _chekNearDoor:Boolean=false;
		private function onWCStatus(status:Object):void 
		{
			TmpModel.wcData.parseStatus(status['WcStatus']);
			var doorName:String = (_forOpen==1)?'left':"right";
			
			// TODO: полюбому открылась(((( нельзя несколько раз посылать одну и ту же команду!!!
			//if (TmpModel.wcData[doorName]!=0)
			//{
				//_openTimer.reset();
				//открылась
				_showArrorTimer.reset();
				_showArrorTimer.start();
				
				this[doorName].visible = true;
			//}
		}		
		
		private function initWithDelay(e:TimerEvent=null):void 
		{
			//TmpModel.screenController.gotoIndexHTML();
			TmpModel.tmpGlobalController.controllerFinishPay();
		}
	}

}